<?php
/*
 *  ┌─────────────────────────────────────────────────────────────┐
 *  │┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐│
 *  ││Esc│!1 │@2 │#3 │$4 │%5 │^6 │&7 │*8 │(9 │)0 │_- │+= │|\ │`~ ││
 *  │├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴───┤│
 *  ││ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{[ │}] │ BS  ││
 *  │├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤│
 *  ││ Ctrl │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  ││
 *  │├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────┬───┤│
 *  ││ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│Shift │Fn ││
 *  │└─────┬──┴┬──┴──┬┴───┴───┴───┴───┴───┴──┬┴───┴┬──┴┬─────┴───┘│
 *  │      │Fn │ Alt │         Space         │ Alt │Win│   HHKB   │
 *  │      └───┴─────┴───────────────────────┴─────┴───┘          │
 *  └─────────────────────────────────────────────────────────────┘
 * 
 * @Date         : 2020-09-02 16:48:44
 * @Author       : 真友
 * @Description  : 本代码由【真友】大力敲打
 */

declare (strict_types = 1);

namespace app\mobile\controller;

use think\facade\Request;
use think\facade\Db;
use think\facade\Cache;

use EasyWeChat\Factory;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\Response\QrCodeResponse;
class Meituan extends \app\mobile\controller\Auth
{

  public function poster()
  {
    $fan = input('fan');
    $vo = Db::name('user_qrcode')
              ->where('type','meituan')
              ->where('uid',$this->uid)
              ->find();
    if ($fan==1) {
      if (!$vo['fan_poster']) {
        $this->createPoster($vo,$fan);
      }
    }elseif ($fan == 2) {
      if (!$vo['fan_h5']) {
        $this->createPoster($vo,$fan);
      }
    }else {
      if (!$vo['poster']) {
        $this->createPoster($vo,$fan);
      }
    }
    $vo = Db::name('user_qrcode')
              ->where('type','meituan')
              ->where('uid',$this->uid)
              ->find();
    return $this->doSuccess('ok',$vo);
  }
  public function createPoster($vo = array(),$fan = 0)
  {
    $root_path = root_path();
    $dir = "/upload/poster/user";
    $poster_fname = "/poster_meituan_{$this->uid}_{$fan}.png";
    
    $poster_path = $root_path.'web'.$dir.$poster_fname;
    $poster_url = Request::domain().$dir.$poster_fname;
    
    $qr_data = '';
    if ($fan == 1) {
      $config = minicfg();
      $app = Factory::miniProgram($config);
      $scene = 'inviteZ'.$this->uid;
      $path = 'views/meituan/poster';
      if (!$fan) {
        $path = 'views/meituan/redbag';
      }

      $response = $app->app_code->getUnlimit($scene, [
        'page'  => $path,
        'width' => 300,
      ]);
      
      if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
        $qr_data = $response->getBodyContents();
      }

    }elseif ($fan == 2) {
      $h5_url = Request::domain().'/h5/#/views/login/index?invite='.$this->uid;
      $qrCode = new QrCode($h5_url);
      $qrCode->setSize(400);
      $qrCode->setWriterByName('png');
      $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
      $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
      $qr_data = $qrCode->writeString();
      // print_r($h5_url);
      // return;
      # code...
    }
    
    // var_dump($response);


    $poster_bg = './upload/tp_poster_meituan.jpg';

    $dir = dirname($poster_path);
    if (!file_exists($dir)) {
      mkdir($dir,0777,true);
    }

    $poster_setting = [
      'bg_url' => $poster_bg,//背景图片路径
      'image'=>[
        [
          'stream' => $qr_data, //图片资源是否是字符串图像流
          'left' => 206,
          'top' => 726,
          'width' => 340,
          'height' => 340,
        ]
      ]
    ];

    if ($fan > 0 && $this->user) {
      if ($this->user['head']) {
        $head = file_get_contents($this->user['head']);
        $poster_setting['image'][] = [
          // 'url' => 'http://api.yunkeq.com/a.png',
          'stream' => $head, //图片资源是否是字符串图像流
          'left' => 20,
          'top' => 1210,
          'width' => 100,
          'height' => 100,
        ];
      }

      $poster_setting['text'][] = [
          'text' => "【".$this->user['nick']."】邀请你点外卖",
          // 'text' => "邀请你一起买",
          'left' => 140,
          'top' => 1270,
          'fontSize' => 20, //字号
          'fontColor' => '255,0,0', //字体颜色
          'angle' => 0,
      ];
    }
    
    \app\common\Poster::setConfig($poster_setting);

    \app\common\Poster::make($poster_path);

    $vv =  array();
    if ($fan == 1) {
      $vv['fan_poster'] = $poster_url;
    }elseif ($fan == 2) {
      $vv['fan_h5'] = $poster_url;
    }else{
      $vv['poster'] = $poster_url;
    }
    Db::name('user_qrcode')->where('id',$vo['id'])->update($vv);
  }
  public function createPoster2()
  {

    $root_path = root_path();
    $dir = '/upload/poster/';
    $qr_fname = "qr_meituan_{$this->uid}.png";
    $poster_fname = "/poster_meituan_{$this->uid}.png";

    $qr_path = $root_path.'web'.$dir.$qr_fname;
    $qr_url = Request::domain().$dir.$qr_fname;

    $waimai = new \app\common\Waimai();
    $query['sid'] = "uidZ{$this->uid}";
    $query['qrcode'] = 'true';
    $query['generate_we_app'] = 'true';
    $res = $waimai->ddx_meituan($query);
    print_r($res);
    return;
    $url = $res['data']['h5'];

    //二维码的生成
    $dir = dirname($qr_path);
    if (!file_exists($dir)) {
      mkdir($dir,0777,true);
    }
    

    $qrCode = new QrCode($url);
    $qrCode->setSize(400);
    $qrCode->setWriterByName('png');
    // $qrCode->setErrorCorrectionLevel(\ErrorCorrectionLevel::HIGH());
    $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
    $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
    $qrCodeData = $qrCode->writeString();
    file_put_contents($qr_path,$qrCodeData);


    $poster_path = $root_path.'web'.$dir.$poster_fname;
    $poster_url = Request::domain().$dir.$poster_fname;

    $poster_bg = './upload/tp_poster_meituan.jpg';
    $qr_data = file_get_contents($qr_path);

    $dir = dirname($poster_path);
    if (!file_exists($dir)) {
      mkdir($dir,0777,true);
    }

    \app\common\Poster::setConfig([
      'bg_url' => $poster_bg,//背景图片路径
      'image'=>[
        [
          // 'url' => $qr_url,
          'stream' => $qr_data, //图片资源是否是字符串图像流
          'left' => 206,
          'top' => 726,
          'width' => 340,
          'height' => 340,
          // 'radius' => 50,
          // 'opacity' => 100
        ]
      ]
    ]);
    \app\common\Poster::make($poster_path);

    $vo =  array();
    $vo['uid'] = $this->uid;
    $vo['ctime'] = time();
    $vo['type'] = 'meituan';
    $vo['qr'] = $qr_url;
    $vo['qr_path'] = $qr_path;
    $vo['poster'] = $poster_url;
    $vo['poster_path'] = $poster_path;

    $ck = Db::name('user_qrcode')->where('type','meituan')->where('uid',$this->uid)->find();
    if ($ck) {
      Db::name('user_qrcode')->where('id',$ck['id'])->update($vo);
    }else{
      Db::name('user_qrcode')->insert($vo);
    }

    return $this->doSuccess('ok',$vo);
  }

  public function doShare()
  {
    $ck = Db::name('user_qrcode')
                ->where('uid',$this->uid)
                ->where('type','meituan')
                ->find();
    $share = getSettingType('美团');
    $share['wenan'] = "外卖红包天天领取，下单更优惠。\n".$ck['click_url'];
    return $this->doSuccess('ok',$share);
  }

  public function mini()
  {

    $uid = input('uid');
    $path = input('path');
    $sid = $this->uid;
    if ($uid) $sid = $uid;


    $ck = Db::name('user_qrcode')
                ->where('uid',$this->uid)
                ->where('type','meituan')
                ->find();
    if ($ck) {
      return $this->doSuccess('ok',$ck);
    }

    $waimai = new \app\common\Waimai();
    $query['sid'] = $sid;
    $query['qrcode'] = 'true';
    $query['generate_we_app'] = 'true';
    $res = $waimai->ddx_meituan($query);
    if ($res['code'] != 200) {
      return $this->doError($res['msg']);
    }

    $vv =  array();
    $vv['uid'] = $this->uid;
    $vv['click_url'] = $res['data']['short_h5'];
    $vv['wx_mini_path'] =  $res['data']['we_app_info']['page_path'];
    $vv['wx_qrcode_url'] = $res['data']['qrcode'];
    $vv['type'] = 'meituan';
    $vv['wx_mini_appid'] = $res['data']['we_app_info']['app_id'];
    $vv['ctime'] = time();
    Db::name('user_qrcode')->insert($vv);
    $ck = Db::name('user_qrcode')->where('uid',$this->uid)->where('type','meituan')->find();
    return $this->doSuccess('ok',$ck);
  }

  
}